package com.jpl.seminar.ui.module.registration;

import java.util.Collection;
import java.util.List;

import org.vaadin.addon.customfield.CustomField;

import com.jpl.seminar.model.Participant;
import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.TableFieldFactory;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

public class ParticipantCustomField extends CustomField {

	private static final long serialVersionUID = 1L;
	private final BeanItemContainer<Participant> container;

	public ParticipantCustomField() {
		VerticalLayout verticalLayout = new VerticalLayout();
		setCompositionRoot(verticalLayout);

		Button addparticipantButton = new Button("Ajouter un participant");
		addparticipantButton.addStyleName(BaseTheme.BUTTON_LINK);
		addparticipantButton.setIcon(new ThemeResource("icons/add.png"));
		verticalLayout.addComponent(addparticipantButton);

		Table participantTable = new Table();
		participantTable.setWidth("50%");
		verticalLayout.addComponent(participantTable);
		participantTable.setTableFieldFactory(new ParticipantTableFieldFactory());
		this.container = new BeanItemContainer<Participant>(Participant.class);
		participantTable.setContainerDataSource(this.container);

		participantTable.addGeneratedColumn("DELETE", new ActionColumnGenerator());

		participantTable.setVisibleColumns(new String[] { ModelPropertiesConstant.PARTICIPANT_FULLNAME_FIELD, ModelPropertiesConstant.PARTICIPANT_POSITION, ModelPropertiesConstant.PARTICIPANT_EMAIL,
				"DELETE" });

		participantTable.setColumnHeaders(new String[] { "Nom", "position", "email", "-" });

		participantTable.setColumnCollapsingAllowed(false);
		participantTable.setSortDisabled(true);
		participantTable.setColumnReorderingAllowed(false);
		participantTable.setSelectable(false);
		participantTable.setPageLength(3);
		participantTable.setEditable(true);

		participantTable.setColumnWidth("DELETE", 40);
		participantTable.setColumnAlignment("DELETE", Table.ALIGN_CENTER);
		participantTable.setColumnExpandRatio(ModelPropertiesConstant.PARTICIPANT_FULLNAME_FIELD, 0.6f);
		participantTable.setColumnExpandRatio(ModelPropertiesConstant.PARTICIPANT_POSITION, 0.2f);
		participantTable.setColumnExpandRatio(ModelPropertiesConstant.PARTICIPANT_EMAIL, 0.2f);
		addparticipantButton.addListener(new Button.ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				@SuppressWarnings("unchecked")
				List<Participant> participants = (List<Participant>) getValue();
				participants.add(new Participant());
				ParticipantCustomField.this.container.removeAllItems();
				ParticipantCustomField.this.container.addAll(participants);
			}
		});

	}

	@Override
	public Class<?> getType() {
		return Collection.class;
	}

	class ActionColumnGenerator implements ColumnGenerator {

		private static final long serialVersionUID = 1L;

		@Override
		public Component generateCell(Table source, Object itemId, Object columnId) {
			Embedded result = new Embedded();
			result.setType(Embedded.TYPE_IMAGE);
			result.setSource(new ThemeResource("icons/delete.png"));
			result.setWidth("16px");
			result.setHeight("16px");
			return result;
		}
	}

	class ParticipantTableFieldFactory implements TableFieldFactory {

		private static final long serialVersionUID = 1L;

		@Override
		public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
			Field result = null;
			String pid = (String) propertyId;
			if (ModelPropertiesConstant.PARTICIPANT_FULLNAME_FIELD.equals(pid)) {
				TextField textField = new TextField();
				textField.setNullRepresentation("");
				textField.setWidth("100%");
				textField.setImmediate(true);
				result = textField;
			} else if (ModelPropertiesConstant.PARTICIPANT_POSITION.equals(pid)) {
				TextField textField = new TextField();
				textField.setNullRepresentation("");
				textField.setWidth("100%");
				textField.setImmediate(true);
				result = textField;
			} else if (ModelPropertiesConstant.PARTICIPANT_EMAIL.equals(pid)) {
				TextField textField = new TextField();
				textField.setNullRepresentation("");
				textField.setWidth("100%");
				textField.setImmediate(true);
				result = textField;

			}
			return result;
		}
	}
}
